<?php
/**
*	@author SignpostMarv Martin
*	@package UHU
*	@subpackage UHU_Color
*/

/**
*	Used for doing color-related tasks
*	@package UHU
*	@subpackage UHU_Color
**/
class uhu_color
{
	protected $color;
	protected $r;
	protected $g;
	protected $b;
	protected static $index = array();
	public function __construct($color)
	{
		if(is_integer($color) === false)
		{
			throw new Exception('Color should be specified as integer.');
		}
		$this->color = $color;
	}
	public static function rand()
	{
		return new self(mt_rand(0x99,0xff) + mt_rand(0x9900,0xffff) + mt_rand(0x990000,0xffffff));
	}
	public function r()
	{
		if(isset($this->r) === false)
		{
			$this->r = 0xff & ($this->color >> 0x10);
		}
		return $this->r;
	}
	public function g()
	{
		if(isset($this->g) === false)
		{
			$this->g = 0xff & ($this->color >> 0x8);
		}
		return $this->g;
	}
	public function b()
	{
		if(isset($this->b) === false)
		{
			$this->b = 0xff & $this->color;
		}
		return $this->b;
	}
	public function __toString()
	{
		return (string)dechex($this->color);
	}
	public function toString()
	{
		return $this->__toString();
	}
	public static function registry($color)
	{
		if(isset(self::$index[$color]) === false)
		{
			self::$index[$color] = new self($color);
		}
		return self::$index[$color];
	}
}
?>